<?php

class service extends REST_Controller {

	private $base_url = 'http://localhost/akupilih/service/';

	function api_get()
	{

		if(!$this->get('id') && !$this->get('all'))
		{
			$this->response(array('error' => 'salah param'));
		}
        // $user = $this->some_model->getSomething( $this->get('id') );
		$users = array(
			1 => array('id' => 1, 'name' => 'Semua data API', 'method' => 'api_get', 'param' => 'all/{string}', 
				'example'=> $this->base_url.'api/all/000', 'format' => 'json/xml'),
			2 => array('id' => 2, 'name' => 'Get API by id', 'method' => 'api_get', 'param' => 'id/{id}', 
				'example'=> $this->base_url.'api/id/1', 'format' => 'json/xml'),
			3 => array('id' => 3, 'name' => 'Get hasil Election', 'method' => 'hasil_get', 'param' => 'hasil/count/{id}', 
				'example'=> $this->base_url.'hasil/count/3', 'format' => 'json/xml', 'detail' => array('1' => 'id jokowi-jk', '2' => 'id prabowo-hatta', '3' => 'get all hasil' )));
		if($this->get('id')){ 
			$user = @$users[$this->get('id')];
		} else{
			$user = $users;
		}

		if($user)
		{
            $this->response($user, 200); // 200 being the HTTP response code
        }

        else
        {
        	$this->response(array('error' => 'User could not be found'), 404);
        }
    }


    function hasil_get(){
    	if(!$this->get('count')){
    		$this->response(array('error' => 'salah param'));
    	}


    	$count = array( 
    		1 => array('id' => '1', 'capres' => 'jokowi-jk', 'hasil' => '2000' ),
    		2 => array('id' => '2', 'capres' => 'prabowo-hatta', 'hasil' => '2000'),
    		3 => array('id' => '3', 'name' => 'total data', 'total'	=> array(1 => array('id' => '1', 'capres' => 'jokowi-jk', 'hasil' => '2000'), 2 => array('id' => '2', 'capres' => 'prabowo-hatta', 'hasil' => '2000')))
    		);

    	if($this->get('count')){ 
    		$count = @$count[$this->get('count')];
    	} else{
    		$count = $count;
    	}

    	$this->response($count, 200);
    }


    
    function user_post()
    {
        //$this->some_model->updateUser( $this->get('id') );
    	$message = array('id' => $this->get('id'), 'name' => $this->post('name'), 'email' => $this->post('email'), 'message' => 'ADDED!');

        $this->response($message, 200); // 200 being the HTTP response code
    }
    
    function user_delete()
    {
    	//$this->some_model->deletesomething( $this->get('id') );
    	$message = array('id' => $this->get('id'), 'message' => 'DELETED!');

        $this->response($message, 200); // 200 being the HTTP response code
    }
    
    function users_get()
    {
        //$users = $this->some_model->getSomething( $this->get('limit') );
    	$users = array(
    		array('id' => 1, 'name' => 'Some Guy', 'email' => 'example1@example.com'),
    		array('id' => 2, 'name' => 'Person Face', 'email' => 'example2@example.com'),
    		3 => array('id' => 3, 'name' => 'Scotty', 'email' => 'example3@example.com', 'fact' => array('hobbies' => array('fartings', 'bikes'))),
    		);

    	if($users)
    	{
            $this->response($users, 200); // 200 being the HTTP response code
        }

        else
        {
        	$this->response(array('error' => 'Couldn\'t find any users!'), 404);
        }
    }


    public function send_post()
    {
    	var_dump($this->request->body);
    }


    public function send_put()
    {
    	var_dump($this->put('foo'));
    }
}